<?php
namespace App\Repositories;
use App\Models\Post;
class PostRepository extends BaseRepository{
    // a Post instance
    protected $post;

    /**
     * create a new PostRepository instance
     * PostRepository constructor.
     * @param $post
     */
    public function __construct(Post $post)
    {
        $this->post = $post;
    }

    /**
     * 获取首页数据 & 后台编辑页面数据
     * @return mixed
     */
    public function getPosts(){
        return $this->post->paginate(15);
    }

    /**
     * 获取单个文章的详细数据
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getDetail($id){
        return $this->post->find($id);
    }

    /**
     * @param $input
     */
    public function store($input){

    }

    /**
     * create of update post
     * @param $post
     * @param $input
     * @param null $post_id
     * @return mixed
     */
    public function savePost($post, $input, $post_id = null){
        $post->category_id = $input['category_id'];
        $post->title = $input['title'];
        $post->summary = $input['summary'];
        $post->content = $input['content'];
        $post->view_count = $input['view_count'];
        $post->status = $input['status'];
        if($post_id){
            $post->id = $post_id;
        }
        $post->save();
        return $post;
    }
}